<?PHP
/*
 * $Id: barge-logging-mail.classes.php 139 2009-12-16 15:29:56Z namba0219 $
 * =============================================================================
 * Barge - web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\logging\mail;

use \ReflectionClass;
use \Exception;

use barge\System;
use barge\utils\ArrayList;
use barge\utils\Config;
use barge\utils\String;
use barge\utils\HashMap;
use barge\utils\ObjectUtils;
use barge\mail\Mailer;

/**
 * メール送信
 *
 * @version 1.0 2009/11/15
 * @see LogAppender
 * @author Kenji Namba
 * @since barge 1.4 2009/11/15
 */
class MailAppender implements LogAppender
{
	const DEFAULT_FORMAT = "{timestamp} {level} - {message}\n";

	private static $posixExists = false;

	/**
	 * @var string
	 */
	private $format;

	/**
	 * @var string
	 */
	private $encoding;

	/**
	 * @var MailProfile
	 */
	private $profile;

	/**
	 * @var MailShutdownHandler
	 */
	private static $handler;

	/**
	 * Constractor.
	 *
	 * @param Config $config
	 * @see LogAppender#__construct()
	 */
	public function __construct(Config $config)
	{
		self::$posixExists = function_exists('posix_getpid');
		$this->format = $config->getProperty('format', self::DEFAULT_FORMAT);
		$this->encoding = $config->getProperty('encoding');
//		$this->profile = $config->getProperty('profile');
		if (self::$handler === null) {
			self::$handler = new MailShutdownHandler();
			System::registerShutdownHandler(self::$handler);
		}

		$host = $config->getProperty('host');
		$to = $config->getProperty('to');
		$subject = $config->getProperty('subject');
		$this->profile = self::$handler->addProfile($host, $to, $subject);
	}

	/**
	 * @param mixed $var
	 * @param string $level レベル文字列
	 * @since 1.0 2006/08/13
	 */
	public function append($var, $level)
	{
		$this->profile->message .=
			String::format($this->format,
			array(
				'level' => strtoupper($level),
				'timestamp' => date("Y/m/d H:i:s"),
				'message' => is_scalar($var) ? (string)$var : ObjectUtils::toString($var),
				'pid' => (self::$posixExists ? posix_getpid() : ''),
				'lf' => "\n",
			));
	}
}


class MailShutdownHandler implements ShutdownHandler
{
	/**
	 * @var ArrayList
	 */
	private $stacks;

	/**
	 * @var Mailer
	 */
	private $mailer;

	public function __construct()
	{
		$this->stacks = new ArrayList();
	}

	/**
	 * @param $args
	 */
	public function shutdown($param)
	{
var_dump($this->stacks);
		foreach ($this->stacks as $profile) {
			@mb_send_mail($profile->getTo(), $profile->getSubject(), $profile->getMessage());
		}
	}

	/**
	 * @param $args
	 * @return  MailProfile
	 */
	public function addProfile($host, $to, $subject)
	{
//		if (!$this->stacks->contains($name)) {
			$profile = new MailProfile();
			$profile->to = $to;
			$profile->host = $host;
			$profile->subject = $subject;
			$this->stacks->add($profile);
			return $profile;
//		}
	}

	/**
	 * @param $args
	 */
	public function getProfile($name)
	{
		return $this->stacks->get($name);
	}
}

class MailProfile
{
	public $message;
	public $to;
	public $subject;
	public $host;

	public function getTo()
	{
		return $this->to;
	}

	public function getSubject()
	{
		return $this->subject;
	}

	public function getMessage()
	{
		return $this->message;
	}
}
